package com.atguigu.gmall.payment.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.payment.service.ZfbPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/***
 * 支付宝支付的controller层
 */
@RestController
@RequestMapping("/zfb/pay")
public class ZfbPayController {

    @Autowired
    private ZfbPayService zfbPayService;

    /**
     * 同步回调返回的接口：提供给支付宝调用，把用户从支付宝带回商城的页面
     * @param callbackReturn
     * @return
     */
    @RequestMapping("/callback/return")
    public String callbackReturn(@RequestParam Map<String, String> callbackReturn){
//        System.out.println("同步回调的内容为 = " + callbackReturn);
        return "同步回调成功,打开支付完成页面!!!!";
    }

    /**
     * 异步回调通知的接口：提供给支付宝调用，通知商城订单的结果
     * @param callbackNotify
     * @return
     */
    @RequestMapping("/callback/notify")
    public String callbackNotify(@RequestParam Map<String,String> callbackNotify){
//        //序列化
//        String callbackNotifyJsonString = JSONObject.toJSONString(callbackNotify);
//        System.out.println("支付后的异步回调结果：" + callbackNotifyJsonString);
        //造一条测试的回调通知结果
        String callbackNotifyJsonString =
                "{\"gmt_create\":\"2023-03-11 09:11:30\",\"charset\":\"utf-8\",\"gmt_payment\":\"2023-03-11 09:11:34\",\"notify_time\":\"2023-03-11 09:11:35\"," +
                        "\"subject\":\"正兴的大玩具111\",\"sign\":\"U/ytzsnKuHkgeN/nDyTnPcxIEf+7GRQKnp//ARWkv+wAiv1Nso5zdPAtT22kbkfQkxi+534nv+z5W7o2L+7LhS9tjace18IkY7We+zemLsKD2IL/iJZBpKXkA/alDOK02+DXOi3F3Hwbey3mJXC6E8mnedADXTWvmrImTu+kxsONbf6nhTWlYPV3LdxtQXXxO5wLudBvJaC8k7E8+QlTvM8+O7uq5zD+LCChq+SvdaO/lmhO6gSXJrtGLBjtUmIjRilbc0xIxp0y/h2ZYC3nVMXSIbgNHtLkg5SM1mK2cTmSpUqUYZwd0Nq/t3lLYLMyyjBpjMsmtg5Qf/ap87HfmQ==\",\"buyer_id\":\"2088032153777146\",\"invoice_amount\":\"0.01\",\"version\":\"1.0\",\"notify_id\":\"2023031101222091135077141452031289\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.01\\\",\\\"fundChannel\\\":\\\"PCREDIT\\\"}]\",\"notify_type\":\"trade_status_sync\"," +
                        "\"out_trade_no\":\"35\",\"total_amount\":\"0.01\",\"trade_status\":\"TRADE_SUCCESS\",\"trade_no\":\"2023031122001477141415191226\",\"auth_app_id\":\"2021001163617452\",\"receipt_amount\":\"0.01\",\"point_amount\":\"0.00\",\"buyer_pay_amount\":\"0.01\",\"app_id\":\"2021001163617452\",\"payment_type\":\"ZFB\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088831489324244\"}";

        //1 调用业务层方法
        zfbPayService.callbackNotify(callbackNotifyJsonString);

        //2 响应支付宝
        return "success";
    }

    /**
     * 查询订单的支付状况
     * @param orderId
     * @return
     */
    @GetMapping("/getPayResult")
    public String getPayResultMap(@RequestParam String orderId){
        //1 校验参数
        if (StringUtils.isEmpty(orderId)) {
            throw new GmallException("参数非法！", null);
        }

        //2 调用业务层的方法
        String payResult = zfbPayService.getPayResultMap(orderId);

        //3 响应数据
        return payResult;
    }

    /**
     * 获取支付宝支付页面
     * @param body
     * @param orderId
     * @param amount
     * @return
     */
    @GetMapping("/getPayPage")
    public String getPayPage(@RequestParam String body,
                             @RequestParam String orderId,
                             @RequestParam String amount){
        //1 校验参数
        if (StringUtils.isEmpty(body) || StringUtils.isEmpty(orderId) || StringUtils.isEmpty(amount)) {
            throw new GmallException("参数非法！", null);
        }

        //2 调用业务层的方法
        String payPage = zfbPayService.getPayPage(body,orderId,amount);

        //3 响应数据
        return payPage;
    }
}
